﻿// JScript File
// RadioButton server in Repeater
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio' && elm.name == nameregex)
        {        
            elm.checked = false;
        }      
   }
   current.checked = true;
}
function InnerHtml(ctr,strHtml){
   if(document.getElementById(ctr)!=null)
   {
      var res = document.getElementById(ctr);
      res.innerHTML = strHtml;
   }   
}
function OnChangeSelectStore()
{
    if(document.getElementById('ddlStore')!=null)
    {
        var selObj = document.getElementById('ddlStore');	
	    var selIndex = selObj.selectedIndex;	
        OnLookupStore(selObj.options[selIndex].value);
    }
}
function OnChangeSelect(iscenter)
{
    if(document.getElementById('selWarranty')!=null)
    {
        var selObj = document.getElementById('selWarranty');	
	    var selIndex = selObj.selectedIndex;	
        OnLookupWarranty(selObj.options[selIndex].value, iscenter);
    }
}
function CheckedRadio(ctr)
{   
   if(document.getElementById(ctr)!=null)
        document.getElementById(ctr).checked = true;
}
function OnLookupWarranty(centerid,iscenter)
{   
  TGDD.Business.ProductSvc.GetDetailWarrantyCenter(centerid,iscenter,OnLookupWarrantyComplete);
}  
function OnLookupWarrantyComplete(result)
{
  if(document.getElementById("div_content_war")!=null)
  {
   var res = document.getElementById("div_content_war");
   res.innerHTML = result;
  }  
}
function OnLookupStore(storeid)
{   
  TGDD.Business.ProductSvc.GetDetailStroe(storeid,OnLookupStoreComplete);
}    
function OnLookupStoreComplete(result)
{
  if(document.getElementById("div_content_stock")!=null){
  var res = document.getElementById("div_content_stock");
  res.innerHTML = result;}
}
function OnlookupOrtherPrice(areaid,productid)
{
  TGDD.Business.ProductSvc.GetPriceByArea(areaid,productid,OnlookupOrtherPriceComplete);
}
function OnlookupOrtherPriceComplete(result)
{
  if(document.getElementById("div_config_list")!=null){
  var res = document.getElementById("div_config_list");
  res.innerHTML = result;}
}
function setHeight(divid,min_height)
{
    if(document.getElementById(divid)!=null)
    {
        var DivHeight = document.getElementById(divid).offsetHeight;
	    if(DivHeight > min_height)
           document.getElementById(divid).style.height = min_height + "px";		
    }
}

function ClosePopup(ctrl)
{
   var mpe = $find(ctrl);
   if (mpe) 
        mpe.hide();
}
function OpenPopup(ctrl)
{
    var mpe = $find(ctrl);
    if (mpe)    
    {      
      mpe.show();          
    }
}    
function ChangeImageSrc(obj,src_images)
{
   if(document.getElementById(obj)!=null){
   var imageS = document.getElementById(obj);
   imageS.src = src_images;}
}

function ChangeColorSrc(divPrePrice, strPrice, imgctrl, src_img) {
   if(document.getElementById(imgctrl)!=null){
        var imageS = document.getElementById(imgctrl);
        imageS.src = src_img;
   }
   
   if (document.getElementById(divPrePrice) != null) {
        if (strPrice != '0') {
            var strPreOrder = "<input id=\"chkOrder\" name=\"chkOrder\" checked=\"checked\" type=\"checkbox\" /><span class=\"div_quick_order\" onclick=\"OpenPopup('QickOrder');\">Đặt hàng online giá rẻ hơn<span class=\"s_price_order\">" + strPrice + "</span><br />(áp dụng giá chuẩn khu vực thành phố Hồ Chí Minh)</span>";
            document.getElementById(divPrePrice).innerHTML = strPreOrder;
        }
        else {
            document.getElementById(divPrePrice).innerHTML = '';
        }
   }
}

function ChangeColorSrc2(divPrePrice, strPrice, imgctrl, src_img) {
    if (document.getElementById(imgctrl) != null) {
        var imageS = document.getElementById(imgctrl);
        imageS.src = src_img;
    }

    if (document.getElementById(divPrePrice) != null) {
        if (strPrice != '0') {
            var strPreOrder = "<input id=\"chkOrder\" name=\"chkOrder\" checked=\"checked\" type=\"checkbox\" /><span class=\"div_quick_order\" onclick=\"OpenPopup('QickOrder');\">Đặt hàng online giá rẻ hơn<span class=\"s_price_order\">" + strPrice + "</span><br />(áp dụng giá chuẩn khu vực thành phố Hồ Chí Minh)</span>";
            document.getElementById(divPrePrice).innerHTML = strPreOrder;
        }
        else {
            document.getElementById(divPrePrice).innerHTML = '';
        }
    }
}

function ChangeStoreSrc(divStatus, divAddress, imgMap, strStatus, strAddress, src_imageMap)
{
    if (document.getElementById(divStatus) != null) {
        document.getElementById(divStatus).innerHTML = strStatus;
    }
    
    if (document.getElementById(divAddress) != null) {
        document.getElementById(divAddress).innerHTML = strAddress;
    }
    
    if(document.getElementById(imgMap)!=null){
        var imageS = document.getElementById(imgMap);
        imageS.src = src_imageMap;
    }
}

function CheckAll(objName,checked)
{
    for (var i=0;i<document.forms[0].elements.length;i++)
    {
        var e = document.forms[0].elements[i];
        if (e.name == objName)
	        e.checked = checked;
    }
}

//  ---------------------------------------

function SelectedAll(objName)
{
    for (var i=0;i<document.forms[0].elements.length;i++)
    {
        var e = document.forms[0].elements[i];
        if (e.name == objName)
	        if(!e.checked)
	            return false;	      
    }
    
    return true;
}

//  ---------------------------------------

function Item_Select(obj, Lobj)
{
   if(document.getElementById(obj)!=null){
        var objTemp = document.getElementById(obj);        
        if(objTemp)
            objTemp.checked = SelectedAll(Lobj);}
}

//  ---------------------------------------

function ConfirmOption(msgEmty, msgQuestion)
{
  if( ! Chk_BeforeDelete(msgEmty, 'iptSelect'))
  return false;
  return confirm(msgQuestion);
}

//  ---------------------------------------

function Chk_BeforeDelete(msgEmty, object)
{
  for (var i = 0; i < document.forms[0].elements.length; i ++ )
  {
    var e = document.forms[0].elements[i];
    if (e.name == object)
    if(e.checked)
    return true;
  }
  alert(msgEmty);
  return false;
}

 //<input id="selectall" name="selectall" title="Chọn để xóa" onclick="javascript:CheckAll('iptSelect', this.checked);"
  //                          type="checkbox" />
  
   //<input id="Checkbox1" name="iptSelect" title="Chọn" onclick="Item_Select('selectall', 'iptSelect');"
     //                       type="checkbox" value='<%#Eval("FormID")%>' />
     
//------------------------------------------

function PerformClick(e, target) 
{
    if (e.which || e.keyCode)
    {
        if ((e.which == 13) || (e.keyCode == 13))
        {
            document.getElementById(target).click();
            return false;
        }
    }
    else
    {
        return true;
    } 
}

//  ---------------------------------------

function ShowHiddenDiv(ctl)
{
    if(document.getElementById(ctl)!=null)
       document.getElementById(ctl).style.display = document.getElementById(ctl).style.display == "none" ? "block" : "none";
}

//  ---------------------------------------

//Truyền vào tên control
function ShowHiddenControl(ctl)
{
    if(document.getElementById(ctl)!=null)
        document.getElementById(ctl).style.display = document.getElementById(ctl).style.display == "none" ? "block" : "none";    
}

//  ---------------------------------------

//An hien 2 coltrol
function CalendarShow(isSelect,ctl1,ctl2)
{
    try
    {
        if(isSelect==1)
        {
            document.getElementById(ctl1).style.display = "none";
            document.getElementById(ctl2).style.display = "block";
        }   
        else
        {
            document.getElementById(ctl1).style.display = "block";
            document.getElementById(ctl2).style.display = "none";
        }
       
    }
    catch(e){  
    }
}

//-----------------------------------------
function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

// Thuc hien voi mot CheckBox cu the khi duoc dua ten
function BeforeAction(msgEmty, msgQuestion,objCheck)
{
  if(!BeforeDelete(msgEmty,objCheck))
  return false;
  return confirm(msgQuestion);
}


//Kiem tra xem co check nao duoc chon hay khong
function BeforeDelete(msgEmty,objCheck)
{
  for (var i = 0; i < document.forms[0].elements.length; i ++ )
  {
    var e = document.forms[0].elements[i];
    if (e.name == objCheck)
    if(e.checked)
    return true;
  }
  alert(msgEmty);
  return false;
}

function ClearText(ctr)
{
    if(document.getElementById(ctr)!=null)
        document.getElementById(ctr).value='';        
}
function SetValues(ctr,_value)
{
   if(document.getElementById(ctr)!=null)
    document.getElementById(ctr).value=_value;        
}

function ObjOnFocus(field,strMess) {
    if(field.value==strMess) {
	    field.value = '';
	    field.className = "txt_focus";
    }
}    
function ObjOnBlur(field,strMess) {
    if(field.value=='')
    {
	    field.value= strMess;
	    field.className = "txt_empty";
    }
}
function setFocus(ctr) {
    if(document.getElementById(ctr)!=null)
        document.getElementById(ctr).focus();  
}
function ShowHideBranch(layer_ref) 
{
   if(document.getElementById(layer_ref)!=null)
   {
        var state = document.getElementById(layer_ref).style.display;
        if (state == 'block') 
        {
            state = 'none';
        }
        else
        {
            state = 'block';
        }
        try
        {
        
            if (document.all) 
            {    
                //IS IE 4 or 5 (or 6 beta)
                eval( "document.all." + layer_ref + ".style.display = state");
            }
            else if (document.getElementById && !document.all) 
            {
                hza = document.getElementById(layer_ref);
                hza.style.display = state;
            }
        }
        catch (e)
        {}
   }
}
function ShowHideAcc(layer_par,layer_ref,css_n,css_ex) 
{
   if(document.getElementById(layer_ref)!=null){
        var state = document.getElementById(layer_ref).style.display;
        var divState = document.getElementById(layer_par);
        if (state == 'block') 
        {
            state = 'none';
            divState.className = css_n;
        }
        else
        {
            state = 'block';
            divState.className = css_ex;
        }
        try
        {    
            if (document.all) 
            {    
                //IS IE 4 or 5 (or 6 beta)
                eval( "document.all." + layer_ref + ".style.display = state");
            }
            else if (document.getElementById && !document.all) 
            {
                hza = document.getElementById(layer_ref);
                hza.style.display = state;
            }
        }
        catch (e)
        {}
    }
}
function ShowHideWithCss(layer_par,layer_ref,css_n,css_ex) 
{
   if(document.getElementById(layer_ref)!=null){
        var state = document.getElementById(layer_ref).style.display;
        var divState = document.getElementById(layer_par);
        if (state == 'block') 
        {
            state = 'none';
            divState.className = css_n;
        }
        else
        {
            state = 'block';
            divState.className = css_ex;
        }
        try
        {    
            if (document.all) 
            {    
                //IS IE 4 or 5 (or 6 beta)
                eval( "document.all." + layer_ref + ".style.display = state");
            }
            else if (document.getElementById && !document.all) 
            {
                hza = document.getElementById(layer_ref);
                hza.style.display = state;
            }
        }
        catch (e)
        {}
    }
}
function changeValue(obj,strValue) {
    if(document.getElementById(obj)!=null)
    {
        var objV = document.getElementById(obj);
        objV.value = strValue;
    }    
}
function ChangeSrcIframe(iframeid,url) {
    var frame = $get(iframeid);
    frame.src = "http://dienmay.com" + url;
}
//change css Name for control
function ChageClassName(obj,css_name) {
  if(document.getElementById(obj)!=null)
    document.getElementById(obj).className = css_name;    
}
//Active Tab
function ActiveTab(tab1, tab2, tab3)
{
    document.getElementById(tab1).style.display="";
    document.getElementById(tab2).style.display="none";
    document.getElementById(tab3).style.display="none";
    document.getElementById(tab1.replace("div", "span")).className="activetab";
    document.getElementById(tab2.replace("div", "span")).className="noactivetab";
    document.getElementById(tab3.replace("div", "span")).className="noactivetab";
}
function ActiveTabTow(tab1, tab2)
{
    document.getElementById(tab1).style.display="";
    document.getElementById(tab2).style.display="none";    
    document.getElementById(tab1.replace("div", "span")).className="activetab";
    document.getElementById(tab2.replace("div", "span")).className="noactivetab";
}
//Cong moi them
function ValidateNumberKeyPress(field, evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   var keychar = String.fromCharCode(charCode);

   if (charCode > 31 && (charCode < 48 || charCode > 57) && keychar != ".")
   {
      return false;
   }
   if (keychar == "." && field.value.indexOf(".") != - 1)
   {
      return false;
   }
   if(keychar == "-")
   {
      if (field.value.indexOf("-") != - 1 /* || field.value[0] == "-" */ )
      {
         return false;
      }
      else
      {
         // save caret position
         var caretPos = getCaretPosition(field);
         if(caretPos != 0)
         {
            return false;
         }
      }
   }

   return true;
}

function getCaretPosition(objTextBox)
{

   var objTextBox = window.event.srcElement;

   var i = objTextBox.value.length;

   if (objTextBox.createTextRange)
   {
      objCaret = document.selection.createRange().duplicate();
      while (objCaret.parentElement() == objTextBox &&
      objCaret.move("character", 1) == 1) -- i;
   }
   return i;
}

function ValidateNumberKeyUp(field,min,max)
{
    if(document.selection.type == "Text")
    {
        return;
    }

     //save caret position
    var caretPos = getCaretPosition(field);
    
    var fdlen = field.value.length;

    UnFormatNumber(field);

    var IsFound = /^-?\d+\.{0,1}\d*$/.test(field.value);
    if(!IsFound)
    {
        setSelectionRange(field, caretPos, caretPos);
        return false;             
    }
    field.value = FormatNumbers(field.value,min,max);
    //field.value = FormatNumbers(field.value,2,',','.','','','-','',min,max);
    
    fdlen = field.value.length - fdlen;

    
    setSelectionRange(field, caretPos+fdlen, caretPos+fdlen);
}

function ValidateAndFormatNumber(NumberTextBox,min,max)
{
    if(NumberTextBox.value == "") return;
    
    UnFormatNumber(NumberTextBox);

    var IsFound = /^-?\d+\.{0,1}\d*$/.test(NumberTextBox.value);
    if(!IsFound)
    {
      //alert("Not a number");
        //NumberTextBox.value = FormatNumbers(min,min,max);
        NumberTextBox.focus();
        NumberTextBox.select();  
        return;             
    }
    
    if(isNaN(parseFloat(NumberTextBox.value)))
    {
        //alert("Number exceeding float range");
        //NumberTextBox.value = FormatNumbers(max,min,max);
        NumberTextBox.focus();
        NumberTextBox.select();               
    }
    NumberTextBox.value = FormatNumbers(NumberTextBox.value,min,max);
//    NumberTextBox.value = FormatNumbers(NumberTextBox.value,2,',','.','','','-','',min,max);
}

function UnFormatNumber(obj)
{
    if(obj.value == "") return;
    
    obj.value = obj.value.replace(/,/gi, "");
}

function setSelectionRange(input, selectionStart, selectionEnd)
{
   if (input.setSelectionRange)
   {
      input.focus();
      input.setSelectionRange(selectionStart, selectionEnd);
   }
   else if (input.createTextRange)
   {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selectionEnd);
      range.moveStart('character', selectionStart);
      range.select();
   }
}

// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
//function FormatNumbers(num, dec, thou, pnt, curr1, curr2, n1, n2,min,max)
function FormatNumbers(fnum,min,max)
{
  if (fnum < min)
      fnum = min.toString();
    if (fnum > max)
        fnum =  max.toString();
    var orgfnum = fnum;
    var flagneg = false;

//    if (fnum.charAt(0) == "-") {
//        flagneg = false;
//        fnum = fnum.substr(1, fnum.length - 1);
//    }    
      psplit = fnum.split(".");

    var cnum = psplit[0],
        parr = [],
        j = cnum.length,
        m = Math.floor(j / 3),
        n = cnum.length % 3 || 3;

    // break the number into chunks of 3 digits; first chunk may be less than 3
    for (var i = 0; i < j; i += n) {
        if (i != 0) { n = 3; }
        parr[parr.length] = cnum.substr(i, n);
        m -= 1;
    }

    // put chunks back together, separated by comma
    fnum = parr.join(",");

    // add the precision back in
    //if (psplit[1]) {fnum += "." + psplit[1];}
    if (orgfnum.indexOf(".") != -1) {
        fnum += "." + psplit[1];
    }

    if (flagneg == true) {
        fnum = "-" + fnum;
    }
    
    return fnum;        
}

function InitRotationTicket(ctrl) {
    var parent = document.getElementById(ctrl);
    var arrChild = new Array();

    var allChild = parent.getElementsByTagName("*");

    for (var i = 0; i < allChild.length; i++) {
        if (allChild[i].className == 'promotionticket') {
            arrChild[arrChild.length] = allChild[i];
        }
    }

    if (arrChild.length > 0) {
        arrChild[0].style.display = 'block';

        for (var i = 1; i < arrChild.length; i++) {
            arrChild[1].style.display = 'none';
        }
    }
}

function RotationTicket(ctrl) {
    var parent = document.getElementById(ctrl);
    var arrChild = new Array();
    var allChild = parent.getElementsByTagName("*");

    for (var i = 0; i < allChild.length; i++) {
        if (allChild[i].className == 'promotionticket') {
            arrChild[arrChild.length] = allChild[i];
        }
    }

    for (var i = 0; i < arrChild.length; i++) {
        if (arrChild[i].style.display == 'block') {
            arrChild[i].style.display = 'none';
            
            if (i < arrChild.length - 1) {
                arrChild[i + 1].style.display = 'block';
            }
            else {
                arrChild[0].style.display = 'block';
            }

            break;
        }
    }
}

